/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PembayaranPanel.java
 *
 * Created on Nov 4, 2014, 3:21:59 PM
 */
package com.ivanbiz.ui;

import com.ivanbiz.dao.GLAccountDAO;
import com.ivanbiz.dao.KamarDAO;
import com.ivanbiz.dao.KosDAO;
import com.ivanbiz.dao.PembayaranDAO;
import com.ivanbiz.dao.impl.GLAccountDAOImpl;
import com.ivanbiz.dao.impl.KamarDAOImpl;
import com.ivanbiz.dao.impl.KosDAOImpl;
import com.ivanbiz.dao.impl.PembayaranDAOImpl;
import com.ivanbiz.model.GLAccount;
import com.ivanbiz.model.Kamar;
import com.ivanbiz.model.Kos;
import com.ivanbiz.model.Pembayaran;
import com.ivanbiz.service.GlobalSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JDialog;
import javax.swing.JOptionPane;

/**
 *
 * @author IW20149X
 */
public class PembayaranPanel extends javax.swing.JPanel {

    JDialog jDialog;
    Pembayaran pembayaran;
    GLAccount gLAccountDebitur;
    List<GLAccount> listKredit;
    List<Kos> listKos;
    List<Kamar> listKamar;
    SimpleDateFormat sdf;

    public PembayaranPanel(JDialog jDialog) {
        initComponents();
        renderKos();
        renderKrediture();
        this.jDialog = jDialog;
        datePembayaran.setDate(new Date());
        sdf = new SimpleDateFormat("yyMMdd");
        textTransaksiRefernece.setText(getTransaksiReference());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel6 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel10 = new javax.swing.JLabel();
        textTransaksiRefernece = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        datePembayaran = new com.toedter.calendar.JDateChooser();
        jLabel7 = new javax.swing.JLabel();
        comboKos = new javax.swing.JComboBox();
        jLabel8 = new javax.swing.JLabel();
        comboKamar = new javax.swing.JComboBox();
        jLabel9 = new javax.swing.JLabel();
        textPenghuni = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        comboKreditur = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        textTarif = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        textDeskripsi = new javax.swing.JTextArea();
        jPanel2 = new javax.swing.JPanel();
        buttonSimpan = new javax.swing.JButton();
        buttonBatal = new javax.swing.JButton();

        jLabel6.setText("jLabel6");

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        jLabel1.setText("Bayar Kamar");

        jPanel1.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));

        jLabel10.setText("Transaksi Reference :");

        textTransaksiRefernece.setEditable(false);

        jLabel2.setText("Tanggal :");

        jLabel7.setText("Kos :");

        comboKos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboKosActionPerformed(evt);
            }
        });

        jLabel8.setText("Kamar:");

        comboKamar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboKamarActionPerformed(evt);
            }
        });

        jLabel9.setText("Penghuni :");

        textPenghuni.setEditable(false);

        jLabel3.setText("Kreditur :");

        jLabel4.setText("Tarif :");

        textTarif.setEditable(false);

        jLabel5.setText("Deskripsi :");

        textDeskripsi.setColumns(20);
        textDeskripsi.setRows(5);
        jScrollPane1.setViewportView(textDeskripsi);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(datePembayaran, javax.swing.GroupLayout.DEFAULT_SIZE, 354, Short.MAX_VALUE)
                    .addComponent(jLabel7)
                    .addComponent(comboKos, 0, 354, Short.MAX_VALUE)
                    .addComponent(jLabel8)
                    .addComponent(comboKamar, 0, 354, Short.MAX_VALUE)
                    .addComponent(jLabel9)
                    .addComponent(textPenghuni, javax.swing.GroupLayout.DEFAULT_SIZE, 354, Short.MAX_VALUE)
                    .addComponent(comboKreditur, 0, 354, Short.MAX_VALUE)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4)
                    .addComponent(textTarif, javax.swing.GroupLayout.DEFAULT_SIZE, 354, Short.MAX_VALUE)
                    .addComponent(jLabel5)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 354, Short.MAX_VALUE)
                    .addComponent(jLabel10)
                    .addComponent(textTransaksiRefernece, javax.swing.GroupLayout.DEFAULT_SIZE, 354, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel10)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(textTransaksiRefernece, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(datePembayaran, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(comboKos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel8)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(comboKamar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel9)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(textPenghuni, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(textTarif, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(comboKreditur, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        buttonSimpan.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/ivanbiz/ui/icon/process.jpg"))); // NOI18N
        buttonSimpan.setText("Proses");
        buttonSimpan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonSimpanActionPerformed(evt);
            }
        });
        jPanel2.add(buttonSimpan);

        buttonBatal.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/ivanbiz/ui/icon/batal.jpg"))); // NOI18N
        buttonBatal.setText("Batal");
        buttonBatal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonBatalActionPerformed(evt);
            }
        });
        jPanel2.add(buttonBatal);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel1)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void buttonBatalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonBatalActionPerformed
        jDialog.dispose();
    }//GEN-LAST:event_buttonBatalActionPerformed

    private void buttonSimpanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonSimpanActionPerformed
        if (pembayaran == null) {
            pembayaran = new Pembayaran();
        }
        pembayaran.setTransactionReference(textTransaksiRefernece.getText());
        pembayaran.setDateCreated(datePembayaran.getDate());
        pembayaran.setDatePosting(datePembayaran.getDate());
        pembayaran.setDebitBankAccount(gLAccountDebitur);
        pembayaran.setKreditBankAccount(listKredit.get(comboKreditur.getSelectedIndex()));
        pembayaran.setJumlah(textTarif.getText().isEmpty() ? (double) 0 : new Double(textTarif.getText()));
        pembayaran.setDeskripsi(textDeskripsi.getText());
        pembayaran.setStatus("0");
        pembayaran.setPathImage("");
        validate(pembayaran);
    }//GEN-LAST:event_buttonSimpanActionPerformed

    private void comboKosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboKosActionPerformed
        try {
            KamarDAO kamarDAO = new KamarDAOImpl();
            listKamar = kamarDAO.getDataByKosAndPenghuni(listKos.get(comboKos.getSelectedIndex()).getId());
            Kamar kamar = new Kamar();
            kamar.setKodeKamar("");
            listKamar.add(0, kamar);
            updateComboKamar();
        } catch (Exception ex) {
            Logger.getLogger(PembayaranPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_comboKosActionPerformed

    private void comboKamarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboKamarActionPerformed
        Kamar kamar = listKamar.get(comboKamar.getSelectedIndex());
        textPenghuni.setText(kamar.getPenghuni() == null ? "" : kamar.getPenghuni().getIdIdentitas() + " - " + kamar.getPenghuni().getNama());
        textTarif.setText(String.valueOf(new Double(kamar.getTarif()).intValue()));
        renderDebitur(kamar);
    }//GEN-LAST:event_comboKamarActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonBatal;
    private javax.swing.JButton buttonSimpan;
    private javax.swing.JComboBox comboKamar;
    private javax.swing.JComboBox comboKos;
    private javax.swing.JComboBox comboKreditur;
    private com.toedter.calendar.JDateChooser datePembayaran;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea textDeskripsi;
    private javax.swing.JTextField textPenghuni;
    private javax.swing.JTextField textTarif;
    private javax.swing.JTextField textTransaksiRefernece;
    // End of variables declaration//GEN-END:variables

    private void validate(Pembayaran pembayaran) {
        if (comboKos.getSelectedIndex() == 0) {
            JOptionPane.showMessageDialog(this, "Kos tidak boleh kosong");
        } else if (comboKamar.getSelectedIndex() == 0) {
            JOptionPane.showMessageDialog(this, "Kamar tidak boleh kosong");
        } else if (listKredit == null) {
            JOptionPane.showMessageDialog(this, "Kreditur tidak boleh kosong");
        } else {
            int response = JOptionPane.showConfirmDialog(this, "Apakah Pembayaran ingin diproses ?", "Konfirmasi", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
            if (response == JOptionPane.YES_OPTION) {
                try {
                    PembayaranDAO pembayaranDAO = new PembayaranDAOImpl();
                    pembayaranDAO.postingJurnalPembayaran(pembayaran);
                    jDialog.dispose();
                } catch (Exception ex) {
                    Logger.getLogger(PembayaranPanel.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }

    private void renderKos() {
        try {
            KosDAO kosDAO = new KosDAOImpl();
            listKos = kosDAO.getAll(Kos.class);
            Kos kos = new Kos();
            kos.setNama("");
            listKos.add(0, kos);
            updateComboKos();
        } catch (Exception ex) {
            Logger.getLogger(PembayaranPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void updateComboKos() {
        Object data[] = new Object[listKos.size()];
        int x = 0;
        for (Kos kos : listKos) {
            data[x] = kos.getNama();
            x++;
        }
        comboKos.setModel(new DefaultComboBoxModel(data));
    }

    private void updateComboKamar() {
        Object data[] = new Object[listKamar.size()];
        int x = 0;
        for (Kamar kamar : listKamar) {
            data[x] = kamar.getKodeKamar();
            x++;
        }
        comboKamar.setModel(new DefaultComboBoxModel(data));
    }

    private void updateComboKreditur() {
        Object data[] = new Object[listKredit.size()];
        int x = 0;
        for (GLAccount gLAccount : listKredit) {
            data[x] = gLAccount.getNameGL() + " A/C No " + gLAccount.getNoGL();
            x++;
        }
        comboKreditur.setModel(new DefaultComboBoxModel(data));
    }

    private void renderKrediture() {
        try {
            GLAccountDAO gLAccountDAO = new GLAccountDAOImpl();
            listKredit = gLAccountDAO.getDataByEquals(GLAccount.class, "groupACC", "Kreditur");
            updateComboKreditur();
        } catch (Exception ex) {
            Logger.getLogger(PembayaranPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void renderDebitur(Kamar kamar) {
        try {
            GLAccountDAO gLAccountDAO = new GLAccountDAOImpl();
            gLAccountDebitur = (GLAccount) gLAccountDAO.getDataByEqual(GLAccount.class, "noGL", kamar.getKos().getKodeKos() + kamar.getKodeKamar());
        } catch (Exception ex) {
            Logger.getLogger(PembayaranPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private String getTransaksiReference() {
        String noTransaksi = null;
        try {
            PembayaranDAO pembayaranDAO = new PembayaranDAOImpl();
            noTransaksi = pembayaranDAO.getLastNoTransaksi();
            if (!"".equals(noTransaksi)) {
                String[] splitNoTransaksi = noTransaksi.split("-");
                int count = Integer.parseInt(splitNoTransaksi[2]);
                count += 1;
                if (count > 99999) {
                    noTransaksi = sdf.format(new Date()) + "-PK" + GlobalSession.getPengguna().getKode() + "-" + String.format("%05d", 1);
                } else {
                    noTransaksi = sdf.format(new Date()) + "-PK" + GlobalSession.getPengguna().getKode() + "-" + String.format("%05d", count);
                }
            } else {
                noTransaksi = sdf.format(new Date()) + "-PK" + GlobalSession.getPengguna().getKode() + "-" + String.format("%05d", 1);
            }
        } catch (Exception ex) {
            Logger.getLogger(PembayaranPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
        return noTransaksi;
    }

}
